package com.tanhua.dubbo.utils;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.MovementTimeLine;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wjs
 * @version 1.0
 * @date 2021/12/26 0026 下午 7:01
 */
@Component
public class TimeLineService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Async //底层使用多线程实现
    public void saveTimeLine(Long userId, ObjectId id) {
        //获取当前登录用户朋友集合
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Friend> friends = mongoTemplate.find(query, Friend.class);

        friends.stream().forEach(friend -> {
            MovementTimeLine timeLine = new MovementTimeLine();
            //设置动态id
            timeLine.setMovementId(id);
            //设置当前登录用户id
            timeLine.setUserId(friend.getUserId());
            //设置当前登录用户朋友id
            timeLine.setFriendId(friend.getFriendId());
            timeLine.setCreated(System.currentTimeMillis());
            mongoTemplate.save(timeLine);
        });
    }
}
